<?php

namespace app\admin\controller;

use support\think\Cache;
use support\Request;
use app\admin\service\admin\MenuService;
use app\admin\service\admin\RoleService;
use app\admin\service\admin\LinkService;

class IndexController extends BaseController
{
    protected array $noNeedLogin = [];

    public function __construct(MenuService $service)
    {
        parent::__construct();
        $this->service = $service;

        $version = input('v', '');
        if (!empty($version) && $this->version != $version) {
            $this->systemVersion($version);
            redirect('/admin/index/index');
        }
    }

    public function index(Request $request)
    {
        return $this->fetch('index/index');
    }

    public function main()
    {
        return $this->fetch('index/main');
    }

    public function menuList(Request $request, RoleService $roleService, LinkService $linkService)
    {
        $user = $this->adminInfo;
        $roleInfo = $roleService->getAdminRoleInfo($user['role_id']);
        $authList = $this->service->getAdminMenuInfo($roleInfo['type'], $roleInfo['url_menuid'], $roleInfo['act_menuid']);
        $link = $linkService->getShowList();

        $user['role_info'] = $roleInfo;
        $user['roles'] = $authList['roles'];
        $user['dict'] = [];

        $data = [
            'menus' => $authList['menus'],
            'user' => $user,
            'link' => $link,
        ];
        return json(['code' => 1, 'msg' => 'ok', 'data' => $data]);
    }

    public function clear(Request $request)
    {
        try {
            Cache::clear();
            return json(['code' => 1, 'msg' => '清理缓存成功']);
        } catch (\Throwable $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public function myInfo(Request $request)
    {

    }

}